package com.bcbim.authentication_interface.util;

import com.bcbim.authentication_interface.user.dto.LoginUser;
import com.bcbim.authentication_interface.user.dto.LoginUserDTO;
import com.bcbim.common.util.AESUtil;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @Description: 设置、获取登录用户工具类
 * @Author haoshuai
 * @Date 2020/11/23
 * @Version V1.0
 **/
public class AuthUtil {

    private static final ThreadLocal<LoginUserDTO> TL_USER = new ThreadLocal<>();

    /**
     * @Description: 将 LoginUser 放入 ThreadLocal
     * @Param: LoginUser user
     * @Author: haoshuai
     * @Date: 2020/11/23
     */
    public static void setUser(LoginUserDTO user) {
        TL_USER.set(user);
    }

    /**
     * @Description: 从 ThreadLocal 获取 LoginUser, id 已经过解密
     * @Return: LoginUser
     * @Author: haoshuai
     * @Date: 2020/11/23
     */
    public static LoginUserDTO getUser() {
        return TL_USER.get();
    }

    /**
     * @Description: 获取请求用户 id (解密)
     * @Return: String
     * @Author: haoshuai
     * @Date: 2020/12/05
     */
    public static String getUserIdDecrypt() {
        LoginUserDTO user = getUser();
        return ObjectUtils.isEmpty(user) ? null : AESUtil.decrypt(user.getId());
    }

    /**
     * @Description: 获取请求用户 token (加密)
     * @Return: String
     * @Author: haoshuai
     * @Date: 2020/12/05
     */
    public static String getTokenEncrypt() {
        LoginUserDTO user = getUser();
        return ObjectUtils.isEmpty(user) ? null : user.getToken();
    }

    /**
     * @Description: 获取请求用户 pointToken
     * @Return: String
     * @Author: haoshuai
     * @Date: 2021/8/24
     */
    public static String getPointToken() {
        LoginUserDTO user = getUser();
        return ObjectUtils.isEmpty(user) ? null : user.getPointToken();
    }

    /**
     * @Description: 获取请求用户 lineToken
     * @Return: String
     * @Author: haoshuai
     * @Date: 2021/8/24
     */
    public static String getLineToken() {
        LoginUserDTO user = getUser();
        return ObjectUtils.isEmpty(user) ? null : user.getLineToken();
    }

    /**
     * @Description: 获取请求用户健康监测token
     * @Return: String
     * @Author: ye
     * @Date: 2021/10/29
     */
    public static String getHealthToken() {
        LoginUserDTO user = getUser();
        return ObjectUtils.isEmpty(user) ? null : user.getHealthToken();
    }

    public static String getPipeLineDownloadToken() {
        LoginUserDTO user = getUser();
        return ObjectUtils.isEmpty(user) ? null : user.getPipeLineDownloadToken();
    }

    public static String getPipePointDownloadToken() {
        LoginUserDTO user = getUser();
        return ObjectUtils.isEmpty(user) ? null : user.getPipePointDownloadToken();
    }


}
